﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using CRM.MODEL;
using CRM.UTIL;

namespace CRM.DAL
{
    public class PartsModelMappingD : DBChannel
    {
        /// <summary>
        /// 可以在此选择数据库连接，默认为CRM
        /// </summary>
        public PartsModelMappingD() : base()
        {
        }


        #region 执行存储过程 Pro_GetPartsModelMapping ,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_GetPartsModelMapping 
        ///xialei
        /// </summary>

        public DataTable ExecuteGetPartsModelMapping(ref CRM.MODEL.GetPartsModelMapping stu)
        {
            //存储过程参数	 		     
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.IDPARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.PARTSIDPARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.MODELIDPARM, SqlDbType.Int));

            //分页参数
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.ORDERSTRING_PARM,
                SqlDbType.VarChar, 1000));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.PAGESIZE_PARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.CURRENTPAGEINDEX_PARM,
                SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsModelMapping.TOTALITEMS_PARM,
                SqlDbType.Decimal));

            //设置参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.IDPARM].Value = stu.IDVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.PARTSIDPARM].Value = stu.PARTSIDVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.MODELIDPARM].Value = stu.MODELIDVALUE;

            //设置分页参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.ORDERSTRING_PARM].Value = stu.ORDERSTRING_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.PAGESIZE_PARM].Value = stu.PAGESIZE_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.CURRENTPAGEINDEX_PARM].Value =
                stu.CURRENTPAGEINDEX_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.TOTALITEMS_PARM].Value = stu.TOTALITEMS_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.TOTALITEMS_PARM].Direction = ParameterDirection.Output;

            //执行
            DataTable dt = this.GetDataByProcedure("dbo.Pro_GetPartsModelMapping");
            //获取数据库的信息
            stu.TOTALITEMS_VALUE = this.sqlCmd.Parameters[CRM.MODEL.GetPartsModelMapping.TOTALITEMS_PARM].Value;

            return dt;
        }

        #endregion

        #region 执行存储过程 Pro_EditPartsModelMapping,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_EditPartsModelMapping 的相关参数
        ///xialei
        /// </summary>
        public int ExecuteEditPartsModelMapping(ref CRM.MODEL.EditPartsModelMapping stu)
        {
            if ((string) stu.ACTION_VALUE == "Update")
            {
                #region Update

                var sqlSb = new StringBuilder();
                sqlCmd.Parameters.Clear();
                sqlSb.Append(" UPDATE PartsModelMapping SET ");

                if (stu.ID_VALUE != DBNull.Value)
                {
                }
                if (stu.PARTSID_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" PartsID=@PartsID,");
                    sqlCmd.Parameters.Add(new SqlParameter("@PARTSID", stu.PARTSID_VALUE));
                }
                if (stu.MODELID_VALUE != DBNull.Value)
                {

                    sqlSb.Append(" ModelID=@ModelID,");
                    sqlCmd.Parameters.Add(new SqlParameter("@MODELID", stu.MODELID_VALUE));
                }

                var sql = sqlSb.ToString().TrimEnd(',');

                sqlCmd.Parameters.Add(new SqlParameter("@ID", stu.ID_VALUE));
                //sqlCmd.CommandText = sqlSb.ToString();
                //sqlCmd.ExecuteNonQuery();
                int i = base.ExecuteSQL(sql + " WHERE ID=@ID");

                return i;

                #endregion
            }
            else
            {
                //动作参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.ACTION_PARM,
                    SqlDbType.VarChar, 20));
                //错误信息参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.ERRMSG_PARM,
                    SqlDbType.VarChar, 100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.OUTID_PARM, SqlDbType.Int));
                ////表字段参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.IDPARM, SqlDbType.Int));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.PARTSIDPARM, SqlDbType.Int));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsModelMapping.MODELIDPARM, SqlDbType.Int));



                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.ACTION_PARM].Value = stu.ACTION_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.ERRMSG_PARM].Direction =
                    ParameterDirection.Output;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.OUTID_PARM].Direction = ParameterDirection.Output;
                //设置参数的值
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.IDPARM].Value = stu.ID_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.PARTSIDPARM].Value = stu.PARTSID_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.MODELIDPARM].Value = stu.MODELID_VALUE;

                int i = this.ExecuteProcedure("dbo.Pro_EditPartsModelMapping"); //执行	
                //获取数据库的信息
                stu.ERRMSG_VALUE = this.sqlCmd.Parameters[CRM.MODEL.EditPartsModelMapping.ERRMSG_PARM].Value;
                return i;
            }
        }

        #endregion
    }
}